package com.dts.yjnb.service.admincontroller;

import com.dts.common.core.domain.AjaxResult;
import com.dts.common.core.page.TableDataInfo;
import com.dts.yjnb.service.domain.Product;
import com.dts.yjnb.service.domainDto.ProductDTO;
import com.dts.yjnb.service.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import com.dts.common.core.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

/**
 * 产品管理 信息操作处理
 *
 * @author dts
 */
@Api(tags = "产品管理相关接口")
@RestController
@RequestMapping("/yjnb/produce")
@Slf4j
public class ProductController extends BaseController
{

    @Autowired
    private IProductService productService;

    /**
     * 产品分页查询（带缓存）
     */
    @GetMapping("/page")
    @ApiOperation("产品分页查询")
    @Cacheable(cacheNames = "productPage", key = "#productDTO.page + '-' + #productDTO.pageSize + '-' + #productDTO.name + '-' + #productDTO.categoryId + '-' + #productDTO.status + '-' + #productDTO.isRecommend")
    public TableDataInfo page(ProductDTO productDTO)
    {
        log.info("产品分页查询:{}", productDTO);
        TableDataInfo tableDataInfo = productService.pageQuery(productDTO);
        return tableDataInfo;
    }

    /**
     * 根据ID查询产品详情
     * @param id
     * @return
     */
    @GetMapping("/getByid/{id}")
    @ApiOperation("根据ID查询产品详情")
    public AjaxResult getById(@PathVariable Long id)
    {
        log.info("根据ID查询产品详情:{}", id);
        Product product = productService.getByid(id);
        return AjaxResult.success(product);
    }

    /**
     * 产品批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deletes")
    @ApiOperation("产品批量删除")
    @CacheEvict(cacheNames = "productPage", allEntries = true)
    public AjaxResult deleteByIds(@RequestParam List<Long> ids)
    {
        log.info("产品批量删除:{}", ids);
        productService.deleteByids(ids);
        return AjaxResult.success("删除成功");
    }

    /**
     * 批量修改产品状态
     * @param ids
     * @param status
     * @return
     */
    @PutMapping("/updateStatus")
    @ApiOperation("批量修改产品状态")
    @CacheEvict(cacheNames = "productPage", allEntries = true)
    public AjaxResult updateStatus(@RequestParam List<Long> ids, @RequestParam Integer status)
    {
        log.info("批量修改产品状态, ids:{}, status:{}", ids, status);
        productService.updateStatusByids(ids, status);
        String message = status == 1 ? "上架成功" : "下架成功";
        return AjaxResult.success(message);
    }

    /**
     * 批量修改产品推荐状态
     * @param ids
     * @param isRecommend
     * @return
     */
    @PutMapping("/updateRecommend")
    @ApiOperation("批量修改产品推荐状态")
    @CacheEvict(cacheNames = "productPage", allEntries = true)
    public AjaxResult updateRecommend(@RequestParam List<Long> ids, @RequestParam Integer isRecommend)
    {
        log.info("批量修改产品推荐状态, ids:{}, isRecommend:{}", ids, isRecommend);
        productService.updateRecommendByids(ids, isRecommend);
        String message = isRecommend == 1 ? "推荐成功" : "取消推荐成功";
        return AjaxResult.success(message);
    }

    /**
     * 产品新增
     * @param productDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("产品新增")
    @CacheEvict(cacheNames = "productPage", allEntries = true)
    public AjaxResult add(@RequestBody ProductDTO productDTO)
    {
        log.info("产品新增:{}", productDTO);
        productService.add(productDTO);
        return AjaxResult.success("新增成功");
    }

    /**
     * 产品编辑
     * @param productDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("产品编辑")
    @CacheEvict(cacheNames = "productPage", allEntries = true)
    public AjaxResult update(@RequestBody ProductDTO productDTO)
    {
        log.info("产品编辑:{}", productDTO);
        productService.update(productDTO);
        return AjaxResult.success("编辑成功");
    }
}
